package cn.gson.crm.control;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.gson.crm.model.dao.DeptDao;
import cn.gson.crm.model.pojo.Dept;
import cn.gson.crm.model.pojo.Emp;
import cn.gson.crm.model.pojo.EmpsRole;
import cn.gson.crm.model.pojo.Function;
import cn.gson.crm.model.pojo.Role;
import cn.gson.crm.model.pojoVo.DeptVo;
import cn.gson.crm.model.pojoVo.EmpVo;
import cn.gson.crm.model.pojoVo.EmpVo2;
import cn.gson.crm.model.pojoVo.RolesVo;
import cn.gson.crm.service.DeptService;
import cn.gson.crm.service.EmpService;
import cn.gson.crm.service.FunctionService;
import cn.gson.crm.service.RoleService;

@Controller
public class IndexController {

	@Autowired
	DeptService ds;
	
	@Autowired
	EmpService es;
	
	@Autowired
	FunctionService fs;
	
	@Autowired
	RoleService rs;
	
	
	@GetMapping("/index")
	public String toIndex() {
		return "taojin/index";
	}
	
	@GetMapping("/emp")
	public String toEmp() {
		return "taojin/emp";
	}
	
	@PostMapping("/addEmp")
	@ResponseBody
	public boolean addEmp(Long deptId,Long empId,String empName,BigDecimal empAge,String empSex,String empJob,String empUsername,String empPassword,String empEmail,String empTel,String empQq,String empStatus) {
		Emp emp = new Emp(empAge, empEmail, empJob, empName, empPassword, empQq, empSex, empStatus, empTel, empUsername);
		return es.addEmp(emp, deptId);
	}
	
	@RequestMapping("/getDeptList")
	@ResponseBody
	public List<Dept> getDeptList(){
		return ds.getDeptList();
	}
	
	@GetMapping("/getEmps")
	@ResponseBody
	public List<Emp> getEmpss(){
		return es.queryAllEmp();
	}
	
	@PostMapping("/getEmps")
	@ResponseBody
	public Map getEmps(int page,int rows){
		return es.queryAll(page, rows);
	}
	
	@GetMapping("/getEmpById")
	@ResponseBody
	public Emp getEmpById(Long empId){
		return es.getEmp(empId);
	}
	
	@GetMapping("/getEmpFormById")
	@ResponseBody
	public EmpVo2 getEmpFormById(Long empId) {
		return es.getEmpFormById(empId);
	}
	
	@RequestMapping("/getEmpRoles")
	@ResponseBody
	public List<Role> getEmpRoles(Long empId){
		return es.getEmpRoles(empId);
	}
	
	@GetMapping("/getEmpRoles")
	@ResponseBody
	public List<Role> getEmpRolesX	(Long empId){
		return es.getEmpRoles(empId);
	}
	
	
	@PostMapping("/addEmpRoles")
	@ResponseBody
	public boolean addEmpRoles(Long empId,HttpServletRequest req) {
		String[] str = req.getParameterValues("rolesId[]");
		return es.addEmpRoles(empId,str);
	}
	
	@PostMapping("/editEmp")
	@ResponseBody
	public boolean editEmp(Long deptId,Long empId,String empName,BigDecimal empAge,String empSex,String empJob,String empUsername,String empPassword,String empEmail,String empTel,String empQq,String empStatus) {
		Emp emp = new Emp(empAge, empEmail, empJob, empName, empPassword, empQq, empSex, empStatus, empTel, empUsername);
		return es.editEmp(deptId, empId, empName, empAge, empSex, empJob, empUsername, empPassword, empEmail, empTel, empQq, empStatus);
	}
	
	@PostMapping("/empEditJob")
	@ResponseBody
	public boolean empEditJob(Long empId,String empJob) {
		return es.empEditJob(empId,empJob);
	}
	
	@GetMapping("/deleteEmp")
	@ResponseBody
	public boolean deleteEmp(Long empId) {
		return es.deleteEmp(empId);
	}
	
	@GetMapping("/dept")
	public String toDept() {
		return "taojin/dept";
	}
	
	@PostMapping("/getDepts")
	@ResponseBody
	public Map getDept(int page,int rows){
		return ds.queryAll(page,rows);
		
	}
	
	@GetMapping("/getDepts")
	@ResponseBody
	public Map getDeptt(int page,int rows){
		return ds.queryAll(page,rows);
		
	}
	
	@PostMapping("/addDept")
	@ResponseBody
	public boolean addDept(String deptName,String deptDescribe) {
			return ds.addDept(deptName, deptDescribe);
	}
	
	@PostMapping("/editDept")
	@ResponseBody
	public boolean editDept(Long deptId,String deptName,String deptDescribe) {
			return ds.editDept(deptId, deptName, deptDescribe);
	}
	
	@GetMapping("/getDeptById")
	@ResponseBody
	public Dept getDeptById(Long deptId){
		return ds.getDept(deptId);
	}
	
	@GetMapping("/deleteDept")
	@ResponseBody
	public boolean deleteDept(Long deptId){
		return ds.deleteDept(deptId);
	}
	
	
	@GetMapping("/roles")
	public String toRole() {
		return "taojin/role";
	}
	
	@PostMapping("/getRoles")
	@ResponseBody
	public Map getRoles(int page, int rows) {
		return rs.queryRole(page,rows);
	}
	
	@PostMapping("/getAllRoles")
	@ResponseBody
	public List<Role> getRoles() {
		return rs.getAllRoles();
	}
	
	@PostMapping("/addRole")
	@ResponseBody
	public boolean addRole(String rName,String rDescribe) {
		return rs.addRole(rName, rDescribe);
	}
	
	@PostMapping("/editRole")
	@ResponseBody
	public boolean editRole(Long rId,String rName,String rDescribe) {
		return rs.editRole(rId, rName, rDescribe);
	}
	
	@GetMapping("/getRoleById")
	@ResponseBody
	public Role getRoleById(Long rId) {
		return rs.getRoleById(rId);
	}
	
	@GetMapping("/deleteRole")
	@ResponseBody
	public boolean deleteRole(Long rId) {
		return rs.deleteRole(rId);
	}
	
	@GetMapping("/getFunctions")
	@ResponseBody
	public List<Function> getFunctions(){
		return fs.getFunctions();
	}
	
	@PostMapping("/addRoleFunctions")
	@ResponseBody
	public boolean addRoleFunctions(Long rId,HttpServletRequest req) {
		String[] str = req.getParameterValues("fId[]");
		return rs.addRoleFunctions(rId,str);
	}
	
	@GetMapping("/getRoleFunctions")
	@ResponseBody
	public List<Function> getRoleFunctions(Long rId){
		return rs.getRoleFunctions(rId);
	}
	
	@GetMapping("/getRoleEmps")
	@ResponseBody
	public List<EmpVo> getRoleEmps(Long rId){
		return rs.getRoleEmps(rId);
	}
	
	
	@GetMapping("/login")
	public String toJSP() {
		return "taojin/login";
	}
	
	@PostMapping("/login")
	@ResponseBody
	public boolean doLogin(String username, String password,HttpSession session) {
		return es.login(username, password,session);
	}
	
	
}
